package md5;

import java.security.NoSuchAlgorithmException;

/**
 * Author: Jason Wu
 * Date  : 2013-11-28
 */
public class Main {
    public static void main(String[] args) {
        String request = "\n" +
                "\n" +
                "    {\n" +
                "    \"num_pages\": 1,\n" +
                "    \"page\": 1,\n" +
                "    \"limits\": 10,\n" +
                "    \"hotels\": [\n" +
                "    {\n" +
                "    \"id\": 609198278,\n" +
                "    \"name\": \"Тест отель\"\n" +
                "    }\n" +
                "    ]\n" +
                "    }\n" +
                "\n";
        System.out.println(getMD5(request.getBytes()));
    }

    public static String getMD5(byte[] source) {
        String result = null;
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            md.update(source);
            byte tmp[] = md.digest();
            char str[] = new char[16 * 2];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            result = new String(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }
}
